<?php

namespace application\member\controller;

use ticky\request;
use ticky\upload;

class spacecp extends \application\common\controller\admin {

    public $paytype = array('1' => '支付宝', '2' => '微信');  //支付类型
    public $order_status = array('0' => '未付款', '1' => '已付款');  //订单状态

    /**
     * 会员中心
     */

    public function index() {
        $memberinfo = get_memberinfo($this->uid);
        $groupinfo = get_groupinfo($memberinfo['groupid']);
        $this->assign('memberinfo', $memberinfo);
        $this->assign('groupinfo', $groupinfo);
        $this->display('spacecp_index');
    }

    /**
     * 用户修改资料
     */
    public function profile() {
        load_extend('form.class.php');
        $uid = request::get('uid', 0);
        if (request::isPost()) {
            $member_profile = db('member_profile')->where(array('uid' => $this->uid))->find();
            if ($member_profile) {
                db('member_profile')->update($_POST, array('uid' => $uid));
                $msg = "修改";
            } else {
                $msg = "添加";
                $_POST['uid'] = $uid;
                db('member_profile')->insert($_POST, false, false, false);
            }
            showmsg($msg . '用户详细信息成功', '/member/spacecp/profile');
        } else {
            $member = db('member')->where(array('uid' => $this->uid))->find();
            $member_profile = db('member_profile')->where(array('uid' => $this->uid))->find();
            if ($member_profile) {
                $item = array_merge($member, $member_profile);
            } else {
                $item = $member;
            }

            $nowy = date('Y');
            $birthyeayhtml = '';
            for ($i = 0; $i < 100; $i++) {
                $they = $nowy - $i;
                if (empty($_GET['all']))
                    $selectstr = $they == $item['birthyear'] ? ' selected=\"selected\"' : '';
                $birthyeayhtml .= "<option value=\"$they\"$selectstr>$they</option>";
            }
            $birthmonthhtml = '';
            for ($i = 1; $i < 13; $i++) {
                if (empty($_GET['all']))
                    $selectstr = $i == $item['birthmonth'] ? ' selected=\"selected\"' : '';
                $birthmonthhtml .= "<option value=\"$i\"$selectstr>$i</option>";
            }
            $birthdayhtml = '';
            for ($i = 1; $i < 29; $i++) {
                if (empty($_GET['all']))
                    $selectstr = $i == $item['birthday'] ? ' selected=\"selected\"' : '';
                $birthdayhtml .= "<option value=\"$i\"$selectstr>$i</option>";
            }
            $this->assign('birthyeayhtml', $birthyeayhtml);
            $this->assign('birthmonthhtml', $birthmonthhtml);
            $this->assign('birthdayhtml', $birthdayhtml);

            $this->assign('item', $item);
            $this->display('spacecp_profile');
        }
    }

    /**
     * 修改头像
     */
    public function avatar() {
        global $_G;
        $uid = $_G['uid'];
        if (request::isPost()) {
            $root_path = SITE_PATH == '/' ? ROOT_PATH : str_replace(SITE_PATH, '', str_replace('\\', '/', ROOT_PATH));

            $x = $_POST['x'];
            $y = $_POST['y'];
            $w = $_POST['w'];
            $h = $_POST['h'];
            //上传文件
            $option = array();
            $option['allowtype'] = array('gif', 'jpg', 'png', 'jpeg');
            $option['filepath'] = './runtime/temp/';
            $upload = new upload($option);
            if ($upload->uploadfile('face')) {
                $fileinfo = $upload->getnewfileinfo();
                $fileinfo['originname'] = safe_replace($fileinfo['originname']);
                $fileinfo['thumb'] = 1;

                $oriImg = $root_path . $fileinfo['filepath'] . $fileinfo['filename'];
                $oriImg1 = $root_path . $fileinfo['filepath'] . '_' . $fileinfo['filename'];
                //图像裁剪
                $big = $root_path . './runtime/avatar/' . get_avatar($uid, 'big');
                $middle = $root_path . './runtime/avatar/' . get_avatar($uid, 'middle');
                $small = $root_path . './runtime/avatar/' . get_avatar($uid, 'small');

                $image = new \ticky\image(1);
                $image->crop($oriImg, $oriImg1, $w, $h, $x, $y);

                $image->thumb($oriImg1, $big, 200, 200);
                $image->thumb($oriImg1, $middle, 120, 120);
                $image->thumb($oriImg1, $small, 48, 48);
                @unlink($oriImg);
                @unlink($oriImg1);
                showmsg($msg . '修改头像成功', '/member/spacecp/avatar');
            } else {
                showmsg($msg . '修改头像失败', '/member/spacecp/avatar');
            }
        } else {
            $this->display('spacecp_avatar');
        }
    }

    /**
     * 用户修改密码
     */
    public function password() {
        if (isset($_POST['dosubmit'])) {
            $uid = request::post('uid', '0');
            $oldpass = request::post('oldpass', '');
            $password = request::post('password', '');
            captcha::instance()->check_code();

            if (!db('member')->where(array('uid' => $uid, 'password' => password($oldpass)))->find()) {
                showmsg('原密码错误!');
            }
            if (!is_password($password)) {
                showmsg('新密码不符合规范!');
            }
            if (db('member')->update(array('password' => password($password)), array('uid' => $uid))) {
                showmsg(L('operation_success'), '', 1);
            } else {
                showmsg(L('operation_failure'));
            }
        }
        $this->display('spacecp_password');
    }

    /**
     * 在线充值
     */
    public function recharge() {
        if (isset($_POST['dosubmit'])) {

        } else {
            $data = db('pay_mode')->field('`id`,`name`,`logo`,`desc`,`version`')->where(array('enabled' => 1))->order('id ASC')->select();
            $this->assign('pay_mode', $data);
            $this->display('spacecp_recharge');
        }
    }

    /**
     * 订单管理
     */
    public function myorder() {
        $ret = db('order')->where(" userid = '" . $this->uid . "' ")->order('id desc')->newpage();
        foreach ($ret['items'] as $row) {
            if ($row['paytime'] == 0) {
                $row['paytime'] = '<span style="color: red;">未支付</span>';
            } else {
                $row['paytime'] = date("Y-m-d H:i:s", $row['paytime']);
            }
            $row['status'] = $this->order_status[$row['status']];
            $row['paytype'] = $this->paytype[$row['paytype']];
            $data[] = $row;
        }
        $this->assign('page', $ret['page']);
        $this->assign('items', $data);
        $this->display('spacecp_myorder');
    }

    /**
     * 入账记录
     */
    public function mypay() {
        $ret = db('pay')->where(" userid = '" . $this->uid . "' ")->order('id desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('spacecp_mypay');
    }

    /**
     * 消费记录
     */
    public function mypayspend() {
        $ret = db('pay_spend')->where(" userid = '" . $this->uid . "' ")->order('id desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('spacecp_mypayspend');
    }

}
